package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author likepei
 * @Date 2020/2/11 15:24
 * @Version v1.0
 * @Description 预约设置的控制器
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    //依赖注入
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 基于日期 设置预约设置对象
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            //调用service, 基于日期 设置OrderSetting对象
            orderSettingService.editNumberByDate(orderSetting);
            //成功, 返回成功信息
            return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //失败, 返回失败信息
            return new Result(false, MessageConst.ORDERSETTING_FAIL);
        }

    }

    /**
     * 日历展示预约设置信息
     * @param date 前端传递过来的日期 2020-2
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            //调用service, 获取给前端ajax返回的数据
            List<Map<String, Integer>> list = orderSettingService.getOrderSettingByMonth(date);
            //查询成功, 返回查询成功的信息
            return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败, 返回查询失败的信息
            return new Result(false, MessageConst.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 文件上传
     * @param multipartFile execl模板文件
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile){
        //日志打印 文件名称
        log.debug(">>>>>>>>>>>>>>> excelFile : {}" + multipartFile.getOriginalFilename());

        try {
            //调用POIUtils中的 读取Excel文件内容的功能, 得到包含多个行数据的 集合
            List<String[]> rowsList = POIUtils.readExcel(multipartFile);
            //调用service层的添加方法
            orderSettingService.add(rowsList);
            //返回成功信息
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //返回失败信息
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }
}
